/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.tutorial

import com.almasb.fxgl.app.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.entity.*
import com.almasb.fxgl.entity.components.*
import com.almasb.fxgl.input.*
import com.almasb.fxgl.physics.*
import javafx.scene.input.*
import javafx.scene.paint.*
import javafx.scene.shape.*
import javafx.scene.text.*
import kotlin.collections.set

/**
 * @author Almas Baimagambetov (almaslvl@gmail.com)
 */
class BasicGameApp : GameApplication() {
	private lateinit var player: Entity
	
	override fun initSettings(settings: GameSettings) {
		settings.width = 600
		settings.height = 600
		settings.title = "Basic Game App"
		settings.version = "0.1"
	}
	
	override fun initGame() {
		player = FXGL.entityBuilder()
			.type(EntityType.PLAYER)
			.at(300.0, 300.0)
			.viewWithBBox("brick.png")
			.with(CollidableComponent(true))
			.buildAndAttach()
		
		FXGL.entityBuilder()
			.type(EntityType.COIN)
			.at(500.0, 200.0)
			.viewWithBBox(Circle(15.0, Color.YELLOW))
			.with(CollidableComponent(true))
			.buildAndAttach()
	}
	
	enum class EntityType {
		PLAYER, COIN
	}
	
	override fun initInput() {
		val input = FXGL.getInput()
		
		input.addAction(object : UserAction("Move Right") {
			override fun onAction() {
				player.translateX(5.0) // move right 5 pixels
				FXGL.getGameState().increment("pixelsMoved", 5)
			}
		}, KeyCode.D)
		
		input.addAction(object : UserAction("Move Left") {
			override fun onAction() {
				player.translateX(-5.0) // move left 5 pixels
				FXGL.getGameState().increment("pixelsMoved", 5)
			}
		}, KeyCode.A)
		
		input.addAction(object : UserAction("Move Up") {
			override fun onAction() {
				player.translateY(-5.0) // move up 5 pixels
				FXGL.getGameState().increment("pixelsMoved", 5)
			}
		}, KeyCode.W)
		
		input.addAction(object : UserAction("Move Down") {
			override fun onAction() {
				player.translateY(5.0) // move down 5 pixels
				FXGL.getGameState().increment("pixelsMoved", 5) //改变游戏属性
			}
		}, KeyCode.S)
		
		input.addAction(object : UserAction("Play Sound") {
			override fun onActionBegin() {
				FXGL.play("drop.wav")
			}
		}, KeyCode.F)
	}
	
	override fun initGameVars(vars: MutableMap<String, Any>) {
		vars["pixelsMoved"] = 0
	}
	
	override fun initPhysics() {
		FXGL.getPhysicsWorld().addCollisionHandler(object : CollisionHandler(EntityType.PLAYER, EntityType.COIN) {
			// order of types is the same as passed into the constructor
			override fun onCollisionBegin(player: Entity?, coin: Entity?) {
				coin?.removeFromWorld()
			}
		})
	}
	
	override fun initUI() {
		val textPixels = Text()
		textPixels.translateX = 50.0 // x = 50
		textPixels.translateY = 100.0 // y = 100
		
		//绑定游戏属性到文本视图
		textPixels.textProperty().bind(FXGL.getGameState().intProperty("pixelsMoved").asString())
		
		FXGL.getGameScene().addUINode(textPixels) // add to the scene graph
		
		val brickTexture = FXGL.getAssetLoader().loadTexture("brick.png")
		brickTexture.translateX = 50.0
		brickTexture.translateY = 450.0
		
		FXGL.getGameScene().addUINode(brickTexture)
	}
}

fun main(args: Array<String>) {
	GameApplication.launch(BasicGameApp::class.java, args)
}
